//
//  RBGetCouponVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/29.
//

#import "RBGetCouponVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import "RBPager.h"
#import "RanTool.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RBGetCouponListVC.h"
#import "UIButton+RBAdd.h"
#import "RBGetCouponResponse.h"

@interface RBGetCouponVC ()

@property(nonatomic, strong)RBPagerView *pagerView;
@property(nonatomic, strong)NSMutableDictionary *vcsDicM;
@property(nonatomic, strong)RBNavigationBar *navigationBar;
@property(nonatomic, strong)UILabel *titleL;

@property(nonatomic, strong) RBGetCouponResponse *response;

@end

@implementation RBGetCouponVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    self.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#4D81FF"];
    self.statusBarStyle = UIStatusBarStyleLightContent;
}

- (RBGetCouponListVC *)vcWithIndex: (NSInteger)index
{
    RBGetCouponListVC *vc = self.vcsDicM[@(index)];
    if (!vc)
    {
        vc = RBGetCouponListVC.new;
        RBGetCouponClassModel *model = self.response.infoclass[index];
        vc.id = model.class;
        [self addChildViewController:vc];
        self.vcsDicM[@(index)] = vc;
    }
    return vc;
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"hu_coupon" post:YES log:YES responseClass:RBGetCouponResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
        weak_self.response = data;
        weak_self.pagerView.hidden = NO;
        [weak_self.view bringSubviewToFront:weak_self.navigationBar];
    } failBlock:^(RBGetCouponResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

#pragma mark - Setter

#pragma mark - Getter

- (RBPagerView *)pagerView
{
    if (!_pagerView)
    {
        self.pagerView = ({
            RBPagerView *view = RBPagerView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            __weak typeof(self) weakSelf = self;
            view.getVcBlock = ^UIViewController * _Nonnull(NSInteger index) {
                return [weakSelf vcWithIndex:index];
            };
            CGFloat stickTop = kRBStatusBarHeight + kRBNavigationBarHeight;
            CGFloat imgH = Zoom(290);
            CGFloat headerH = Zoom(66);
            [view configWithHeaderStickTop:stickTop headerBottom:0 headerHeight:headerH floatViewHeight:stickTop + imgH +headerH];
            [view setupWithModel:({
                RBPagerHeaderModel *model = RBPagerHeaderModel.new;
                model.titles = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (RBGetCouponClassModel *model in self.response.infoclass)
                    {
                        [arrayM addObject:model.class_title];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForNormalState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14)] color:[RBThemeConfig textGrayColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForSelectedState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(16) weight:UIFontWeightMedium] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.backgroundColor = UIColor.clearColor;
                model.showSeperator = NO;
                model.buttonWidthStyle = RBPagerButtonWidthTitleWidthPlusPadding;
                model.headerButtonPadding = Zoom(24);
                model.bottomLineWidthStyle = RBPagerBottomLineWidthConstValue;
                model.bottomLineColor = [UIColor colorWithHexString:@"#518CFF"];
                model.bottomLineWidth = Zoom(22);
                model.bottomLineHeight = Zoom(3);
                model.bottomLineBottomMargin = Zoom(17);
                model;
            })];
            view.yProgressBlock = ^(CGFloat yProgress) {
                weakSelf.titleL.textColor = yProgress > 0.5? [RBThemeConfig textBlackColor]: UIColor.whiteColor;
                [[weakSelf.navigationBar.naviBar getBackBtn] rbSetImage:[UIImage imageNamed:yProgress > 0.5? @"backBlack": @"backArror"]];
                weakSelf.statusBarStyle = yProgress > 0.5? UIStatusBarStyleDefault: UIStatusBarStyleLightContent;
                weakSelf.navigationBar.backgroundAlpha = yProgress;
            };
            
            [view.pagerHeaderView.bottomLineView rbSetCornerRadius:Zoom(1.5)];
            
            view.floatView.backgroundColor = UIColor.whiteColor;
            
            UIView *topView = ({
                UIView *view = UIView.new;
                
                UIImageView *imgView = [RanTool imgViewWithMode:0 radius:0 imgName:@"takeCouponTopBg"];
                [view addSubview:imgView];
                [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.bottom.mas_equalTo(0);
                    make.height.mas_equalTo(Zoom(388));
                }];
                                
                view;
            });
            [view.floatView addSubview:topView];
            [topView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(stickTop + imgH);
            }];
            
            UIImageView *headerBgImgView = [RanTool imgViewWithMode:0 radius:0 imgName:@"takeCouponGra"];
            [view.floatView addSubview:headerBgImgView];
            [view.floatView sendSubviewToBack:headerBgImgView];
            [headerBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(headerH);
            }];
            
            view;
        });
    }
    return _pagerView;
}

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *view = RBNavigationBar.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            
            view.needGradientView = NO;
            view.backgroundAlpha = 0;
            view.bgColor = UIColor.whiteColor;
            
            [view.naviBar addBackBtn];
            UILabel *titleL = [RanTool labelWithFontSize:18 weight:0 color:UIColor.whiteColor align:0 text:@"领券"];
            self.titleL = titleL;
            [view.naviBar addSubview:titleL];
            [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(0);
            }];
            
            view;
        });
    }
    return _navigationBar;
}

- (NSMutableDictionary *)vcsDicM
{
    if (!_vcsDicM)
    {
        self.vcsDicM = NSMutableDictionary.dictionary;
    }
    return _vcsDicM;
}

@end
